package com.chihihx.launcher

import android.graphics.drawable.Drawable
import android.view.KeyEvent
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.lifecycleScope
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import io.github.lib_autorun.ad.AdSdk
import com.shudong.lib_base.ext.appContext
import com_github_lib_base.base.BaseVMActivity
import com_github_lib_base.base.BaseViewModel
import jp.wasabeef.glide.transformations.BlurTransformation
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume

open class BaseWallpaperActivity<VDB : ViewDataBinding, VM : BaseViewModel> :
    BaseVMActivity<VDB, VM>() {


    override fun initBeforeBaseLayout() {
        rootBinding.layoutRoot.background = localWallPaperDrawable
        loadLayout()

    }

    override fun onStart() {
        super.onStart()
        updateWallPaper()
    }


    var job: Job? = null
    var lastResId = 0
    fun updateWallPaper() {
        if (lastResId != curWallpaper()) {
            job?.cancel()
            job = lifecycleScope.launch {
                val resId = curWallpaper()
                val drawable = resId.loadBlurDrawable()
                localWallPaperDrawable = drawable

                rootBinding.layoutRoot.background?.let {
                    rootBinding.layoutRoot.background = localWallPaperDrawable
                }
                if (rootBinding.layoutRoot.background == null) {
                    rootBinding.layoutRoot.background = localWallPaperDrawable
                }
                lastResId = resId
            }
        }
    }

}

var localWallPaperDrawable:Drawable?=null
fun curWallpaper() = run {
    val wallpaperResId =  AppCache.wallpaper
    wallpaperResId
}

/*
*
* BlurTransformation(5, 1) → 轻微模糊
  BlurTransformation(15, 2) → 比较模糊
  BlurTransformation(25, 4) → 很模糊、性能更好
* */
suspend fun Any.loadBlurDrawable(applyBlur: Boolean = true): Drawable? {
    return suspendCancellableCoroutine { continuation ->
        val requestOptions = if (applyBlur) {
            RequestOptions.bitmapTransform(BlurTransformation(15, 2))
                .override(320, 180)
        } else {
            RequestOptions() // 不做模糊
        }

        Glide.with(appContext)
            .load(this)
            .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
            //.override(320, 180)
            .apply(requestOptions)
            .into(object : CustomTarget<Drawable>() {
                override fun onResourceReady(
                    resource: Drawable,
                    transition: Transition<in Drawable>?
                ) {
                    continuation.resume(resource)
                }

                override fun onLoadCleared(placeholder: Drawable?) {
                    continuation.resume(null)
                }
            })
    }
}